/**
 * 宽带预约列表
 */
(function($){
  var pageNumber = 1;
  var totalPage = 0;
  var oldKeyword = "";
  var userId = getParamValue("userId");
  if(!userId){
    userId = getParamValue("Userid");//兼容老版本
  }

  /*
  设置分页参数
  */
  var setPageInfo = function(data){
    totalPage = data.totalPage;
    $("#total-row").html(data.totalRow);
    if(data.totalRow == 0){
      $("#load-more").addClass("am-hide");
      $("#no-more").removeClass("am-hide");
    }else{
      $("#no-more").addClass("am-hide");
      $("#load-more").removeClass("am-hide"); 
    }
  };
  /*
  显示数据
  */
  var showDatas = function(data){
    setPageInfo(data);

  	var $tpl = $('#amz-tpl');
    var source = $tpl.text();
    var template = Handlebars.compile(source);
    var myData = {
    	list: data.list
    };

    var html = template(myData);
    //console.log("html = "+html);
    $("#order-list").html($("#order-list").html() + html);
    //console.log($("#order-list").html());
    //$tpl.before(html);

    //bindEvent();
  };
  /*
  加载数据
  */
  var loadDatas = function(){
  	//$('#modal').modal('open');
  	params = {"pageNumber":pageNumber,"pageSize":3,"userId":userId,"token":getParamValue("token"),"keyWord":oldKeyword};
  	$.ajax({
	    type: "post",
	    url: "../broadbandH5/orderList",
	    data: params,
	    dataType: "json",
	    headers: {
      'token': getParamValue('token'),
      'saleType': getParamValue('saleType')
    },
    success: function(data) {
      if (data.ResultCode === 1009) {
        alert('登陆超时，请重新登录！');
        jumpToSignin();
        return;
      }
	    	//$('#modal').modal('close');
	      console.info(data);
        if(data.resultCode == 1000){
  	      showDatas(JSON.parse(data.data));
        }else{
          alert(data.message);
        }
	    },
	    error : function(textStatus) {
	    	//$('#modal').modal('close');
	        //alert("获取宽带列表信息失败！");
	        console.info("request error");
	    }
	  });
  };
  $(function(){
    /*搜索按钮*/
    $("#search-icon,#search-btn").on("click",function(){
      oldKeyword = $("#keyword").val();
      pageNumber = 1;
      $("#order-list").html("");
      $("#total-row").html("");
      $("#load-more").addClass("am-hide");
      $("#no-more").addClass("am-hide");
      loadDatas();
    });
    $("#load-more").on("click",function(){
      if(pageNumber < totalPage){
        if($("#keyword").val() != oldKeyword){
          $("#keyword").val(oldKeyword);
        }
        pageNumber = pageNumber + 1;
        loadDatas();
      }else{
        $(this).addClass("am-hide");
        $("#no-more").removeClass("am-hide");
      }
    });
    // $("#keyword").on("change",function(){
    //   alert("aaa");
    //   if($(this).val() != oldKeyword){
    //     $("#load-more").addClass("am-hide");
    //   }
    // });
    /*加载模态窗口*/
  	//var options = {closeViaDimmer: false};
    //$('#modal').modal(options);
    
    loadDatas();
  });
})(jQuery);
